package net.xinshi.controller;

import com.alibaba.fastjson.JSON;
import net.xinshi.boot.AppConfig;
import net.xinshi.domain.ApiLogRequest;
import net.xinshi.domain.CommonIdRequest;
import net.xinshi.domain.JSONResponseObj;
import net.xinshi.exception.ApiException;
import net.xinshi.model.ApiLogOutputDTO;
import net.xinshi.model.ApiLogPageInputDTO;
import net.xinshi.model.BaseIdInputDTO;
import net.xinshi.model.BasePageOutputDTO;
import net.xinshi.service.business.IApiLogService;
import net.xinshi.utils.BeanUtils;
import net.xinshi.utils.ErrorCode;
import org.apache.commons.lang.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

/**
 * @author <a href="mailto:liukuo@xinshi.net">liukuo</a>
 * @version 1.0
 * @description
 * @since 2017/6/29.
 */

@RestController
@RequestMapping("/apilog/")
public class ApiLogController extends AbstractRestController {
    private static final Logger LOGGER = LogManager.getLogger(ApiLogController.class);
    @Autowired
    private IApiLogService apiLogService;

    @Autowired
    private AppConfig appConfig;

    @RequestMapping(value = "/logs", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public JSONResponseObj getApiLogList(@RequestBody ApiLogRequest req) {
        LOGGER.info("请求获取API日志分页查询,请求参数是:" + JSON.toJSONString(req));
        try {
            if (!StringUtils.equals(req.getToken(), appConfig.getLogToken())) {
                throw new ApiException("E000001", "你无权限进行此操作");
            }
            if (req.getPageSize() == null) {
                req.setPageSize(10);
            }

            if (req.getStart() == null) {
                req.setStart(0);
            }

            ApiLogPageInputDTO input = BeanUtils.beanCopy(req, ApiLogPageInputDTO.class);
            BasePageOutputDTO<ApiLogOutputDTO> output = apiLogService.queryApiLogList(input);

            JSONResponseObj res = new JSONResponseObj();
            res.setCode(output.getCode());
            res.setMsg(output.getMsg());
            res.setData(output);
            return res;
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("请求获取API日志分页查询，code: {}, msg: {}", ErrorCode.ERR_UNKNOWN_ERROR.getCode(),
                    e.getMessage());
            if (e instanceof ApiException) {
                ApiException ex = (ApiException) e;
                return new JSONResponseObj(ex.getCode(), ex.getMessage());
            }
            return new JSONResponseObj(ErrorCode.ERR_UNKNOWN_ERROR.getCode(), ErrorCode.ERR_UNKNOWN_ERROR.getMsg());
        }
    }

    @RequestMapping(value = "/get", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public JSONResponseObj getApiLog(@RequestBody CommonIdRequest req) {
        LOGGER.info("请求获取API日志,请求参数是:" + JSON.toJSONString(req));
        try {
            BaseIdInputDTO input = BeanUtils.beanCopy(req, BaseIdInputDTO.class);
            ApiLogOutputDTO output = apiLogService.getLog(input);

            JSONResponseObj res = new JSONResponseObj();
            res.setCode(output.getCode());
            res.setMsg(output.getMsg());
            res.setData(output);
            return res;
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("请求获取API日志，code: {}, msg: {}", ErrorCode.ERR_UNKNOWN_ERROR.getCode(),
                    e.getMessage());
            if (e instanceof ApiException) {
                ApiException ex = (ApiException) e;
                return new JSONResponseObj(ex.getCode(), ex.getMessage());
            }
            return new JSONResponseObj(ErrorCode.ERR_UNKNOWN_ERROR.getCode(), ErrorCode.ERR_UNKNOWN_ERROR.getMsg());
        }
    }
}
